/** test JXR FILTER FOR HDF5
 **
 ** 
 **/

#include <H5Zjxr.c>
#include <stdio.h>

//#define INFILE "infile.jxr"
#define INFILE "infile_double.raw"
//#define INFILE "outfile3.jxr"
#define OUTFILE "outfile4.raw"
#define BYTESPERPIXEL 2
#define DIM0 203
#define DIM1 495
#define QUALITY 1

int main (void)
{

    int width = DIM0;
    int height = DIM1;
    U8 quality = QUALITY;
    int bpp = BYTESPERPIXEL;
    size_t totalmem = bpp*height*width;

    // load in raw image for testing
	U16* pixels;
	pixels = malloc(totalmem);
    //PKAllocAligned(&pixels, totalmem, 128);

	FILE *fp;
	fp = fopen( INFILE , "rb" );
	fread(pixels,bpp,width*height,fp);

    size_t cd_nelmts = 3;
    unsigned int cd_values[cd_nelmts];

    cd_values[0] = width;
    cd_values[1] = height;
    cd_values[2] = quality;

    size_t nbytes = totalmem;
    size_t *buf_size;
    buf_size = &totalmem;
    printf("%d\n",(size_t)(totalmem));

    void **buf = &pixels;

    // compress image
    H5Z_filter_jxr(0,cd_nelmts,cd_values,nbytes,buf_size,buf);

    // decompress image
    H5Z_filter_jxr(1,cd_nelmts,cd_values,nbytes,buf_size,buf);

    // save output file and compare to input (should be the same)
    FILE* file_ptr;
    file_ptr = fopen(OUTFILE, "wb");
    fwrite(*buf, 1, *buf_size, file_ptr);
    fclose(file_ptr); 
    return 0;
}
